<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<ul>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#server-requirements">服务器要求</a></li>
<li><a href="#installing-laravel">安装 Laravel</a></li>
<li><a href="#configuration">配置</a></li>
</ul>
</li>
<li><a href="#web-server-configuration">Web 服务器配置</a>
<ul>
<li><a href="#pretty-urls">优雅链接</a></li>
</ul>
</li>
</ul>
<h2 id="安装-1" tabindex="-1"><a class="header-anchor" href="#安装-1"><span>安装</span></a></h2>
<h3 id="服务器要求" tabindex="-1"><a class="header-anchor" href="#服务器要求"><span>服务器要求</span></a></h3>
<p>Laravel 对系统有一些要求。当然，所有这些要求 <a href="https://learnku.com/docs/laravel/6.x/homestead" target="_blank" rel="noopener noreferrer">Laravel Homestead</a> 虚拟机都能满足，因此强烈推荐你使用 Homestead 做为你的开发环境。</p>
<p>当然，假如你不使用 Homestead，请确保你的服务器满足以下要求：</p>
<ul>
<li>PHP &gt;= 7.2.5</li>
<li>BCMath PHP 拓展</li>
<li>Ctype PHP 拓展</li>
<li>JSON PHP 拓展</li>
<li>Mbstring PHP 拓展</li>
<li>OpenSSL PHP 拓展</li>
<li>PDO PHP 拓展</li>
<li>Tokenizer PHP 拓展</li>
<li>XML PHP 拓展</li>
</ul>
<h3 id="安装-laravel" tabindex="-1"><a class="header-anchor" href="#安装-laravel"><span>安装 Laravel</span></a></h3>
<p>Laravel 使用 <a href="https://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a> 来管理项目依赖。因此，在使用 Laravel 之前，请确保你的机器已经安装了 Composer。</p>
<h4 id="通过-laravel-安装器" tabindex="-1"><a class="header-anchor" href="#通过-laravel-安装器"><span>通过 Laravel 安装器</span></a></h4>
<p>首先，通过使用 Composer 安装 Laravel 安装器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">global</span> <span class="token keyword">require</span> laravel<span class="token operator">/</span>installer</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>确保将 Composer’s system-wide vendor 目录放置在你的系统环境变量 <code v-pre>$PATH</code> 中，以便系统可以找到 Laravel 的可执行文件。该目录根据你的操作系统存在不同的位置中；一些常见的配置包括 :</p>
<ul>
<li>macOS and GNU / Linux 发行版: <code v-pre>$HOME/.config/composer/vendor/bin</code></li>
<li>Windows: <code v-pre>%USERPROFILE%\AppData\Roaming\Composer\vendor\bin</code></li>
</ul>
<p>安装完成后， <code v-pre>laravel new</code> 命令会在你指定的目录创建一个全新的 Laravel 项目。例如， <code v-pre>laravel new blog</code> 将会创建一个名为 <code v-pre>blog</code> 的目录，并已安装好 Laravel 所有的依赖项:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">laravel <span class="token keyword">new</span> <span class="token class-name">blog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="通过-composer-创建项目" tabindex="-1"><a class="header-anchor" href="#通过-composer-创建项目"><span>通过 Composer 创建项目</span></a></h4>
<p>或者，你也可以在终端中运行 <code v-pre>create-project</code> 命令来安装 Laravel：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer create<span class="token operator">-</span>project <span class="token operator">--</span>prefer<span class="token operator">-</span>dist laravel<span class="token operator">/</span>laravel blog <span class="token string double-quoted-string">"6.*"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="本地开发环境" tabindex="-1"><a class="header-anchor" href="#本地开发环境"><span>本地开发环境</span></a></h4>
<p>如果你在本地安装了 PHP， 并且你想使用 PHP 内置的服务器来为你的应用程序提供服务，则可以使用 Artisan 命令 <code v-pre>serve</code> 。该命令会在 <code v-pre>http://localhost:8000</code>上启动开发服务器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan serve</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，最好还是选择 <a href="https://learnku.com/docs/laravel/6.x/homestead" target="_blank" rel="noopener noreferrer">Homestead</a> 和 <a href="https://learnku.com/docs/laravel/6.x/valet" target="_blank" rel="noopener noreferrer">Valet</a>。</p>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<h4 id="公共路径" tabindex="-1"><a class="header-anchor" href="#公共路径"><span>公共路径</span></a></h4>
<p>安装完 Laravel 之后，你应该配置你的 web 服务的文档目录指向 <code v-pre>public</code> 路径。该路径下的 <code v-pre>index.php</code> 文件作为进入应用的所有 HTTP 请求的前端控制器。</p>
<h4 id="配置文件" tabindex="-1"><a class="header-anchor" href="#配置文件"><span>配置文件</span></a></h4>
<p>Laravel 框架的所有配置文件存放在 <code v-pre>config</code> 目录下。每个选项都有文档标注，便于通过文件查看并熟悉对你有用的选项。</p>
<h4 id="目录权限" tabindex="-1"><a class="header-anchor" href="#目录权限"><span>目录权限</span></a></h4>
<p>在安装 Laravel 后，你可能需要配置一些权限。 <code v-pre>storage</code> 和 <code v-pre>bootstrap/cache</code> 目录在你的 web 服务下应该是可写的权限，否则 Laravel 将无法运行。如果你用的是 <a href="https://learnku.com/docs/laravel/6.x/homestead" target="_blank" rel="noopener noreferrer">Homestead</a> 虚拟机，这些权限应该已经设置好了。</p>
<h4 id="应用密钥" tabindex="-1"><a class="header-anchor" href="#应用密钥"><span>应用密钥</span></a></h4>
<p>安装好 Laravel 之后的下一步是设置你的应用密钥为随机字符串。如果你通过 Composer 或者 Laravel 安装器安装的，这个密钥已经通过 <code v-pre>php artisan key:generate</code> 命令为你设置好了。</p>
<p>通常，这个字符串应该是 32 个字符长度。这个密钥将会设置在环境变量文件 <code v-pre>.env</code> 中。如果你还没有将 <code v-pre>.env.example</code> 文件重命名为 <code v-pre>.env</code> 那么你现在应当把此文件重命名。 <strong>如果应用密钥还没有设置，你的用户会话和其他的加密数据将会不安全</strong></p>
<h4 id="其他配置" tabindex="-1"><a class="header-anchor" href="#其他配置"><span>其他配置</span></a></h4>
<p>Laravel 几乎不需要除上面所说的其他什么配置了。你可以随心所欲的开始开发了！然而，你可能会想要再次查看 <code v-pre>config/app.php</code> 文件和它的注释说明。它包含一些你希望根据你应用来更改的选项，诸如： <code v-pre>timezone</code> 和 <code v-pre>locale</code> 。</p>
<p>你还可能想要配置 Laravel 的其他的一些组件，例如：</p>
<ul>
<li><a href="https://learnku.com/docs/laravel/6.x/cache#configuration" target="_blank" rel="noopener noreferrer">缓存</a></li>
<li><a href="https://learnku.com/docs/laravel/6.x/database#configuration" target="_blank" rel="noopener noreferrer">数据库</a></li>
<li><a href="https://learnku.com/docs/laravel/6.x/session#configuration" target="_blank" rel="noopener noreferrer">会话控制</a></li>
</ul>
<h2 id="web-服务器配置" tabindex="-1"><a class="header-anchor" href="#web-服务器配置"><span>Web 服务器配置</span></a></h2>
<h3 id="优雅链接" tabindex="-1"><a class="header-anchor" href="#优雅链接"><span>优雅链接</span></a></h3>
<h4 id="apache" tabindex="-1"><a class="header-anchor" href="#apache"><span>Apache</span></a></h4>
<p>Laravel 中包含了一个 <code v-pre>public/.htaccess</code> 文件，通常用于在资源路径中隐藏 <code v-pre>index.php</code> 的前端控制器。在用 Apache 为 Laravel 提供服务之前，确保启用了 <code v-pre>mod_rewrite</code> 模块，这样 <code v-pre>.htaccess</code> 文件才能被服务器解析。</p>
<p>如果 Laravel 附带的 <code v-pre>.htaccess</code> 文件不起作用，尝试下面的方法替代：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Options <span class="token operator">+</span>FollowSymLinks <span class="token operator">-</span>Indexes</span>
<span class="line">RewriteEngine On</span>
<span class="line"></span>
<span class="line">RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">HTTP</span><span class="token punctuation">:</span>Authorization<span class="token punctuation">}</span> <span class="token operator">.</span></span>
<span class="line">RewriteRule <span class="token operator">.</span><span class="token operator">*</span> <span class="token operator">-</span> <span class="token punctuation">[</span><span class="token constant">E</span><span class="token operator">=</span><span class="token constant">HTTP_AUTHORIZATION</span><span class="token punctuation">:</span><span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">HTTP</span><span class="token punctuation">:</span>Authorization<span class="token punctuation">}</span><span class="token punctuation">]</span></span>
<span class="line"></span>
<span class="line">RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>d</span>
<span class="line">RewriteCond <span class="token operator">%</span><span class="token punctuation">{</span><span class="token constant">REQUEST_FILENAME</span><span class="token punctuation">}</span> <span class="token operator">!</span><span class="token operator">-</span>f</span>
<span class="line">RewriteRule <span class="token operator">^</span> index<span class="token operator">.</span>php <span class="token punctuation">[</span><span class="token constant">L</span><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="nginx" tabindex="-1"><a class="header-anchor" href="#nginx"><span>Nginx</span></a></h4>
<p>如果你使用 Nginx ，在你的站点配置中加入以下配置，所有的请求将会引导至 <code v-pre>index.php</code> 前端控制器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">location <span class="token operator">/</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name type-declaration">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span><span class="token operator">/</span> <span class="token operator">/</span>index<span class="token operator">.</span>php<span class="token operator">?</span><span class="token variable">$query_string</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当你使用 <a href="https://learnku.com/docs/laravel/6.x/homestead" target="_blank" rel="noopener noreferrer">Homestead</a> 或 <a href="https://learnku.com/docs/laravel/6.x/valet" target="_blank" rel="noopener noreferrer">Valet</a> 时，优雅链接将会自动配置好。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/installation/5124" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/ins...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/installation/5124" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/ins...</a></p>
</blockquote>
</div></template>


